package cn.tedu.smartos.health.controller;

import cn.tedu.smartos.base.response.JsonResult;
import cn.tedu.smartos.health.pojo.dto.HealthAlertParam;
import cn.tedu.smartos.health.pojo.vo.HealthAlertVO;
import cn.tedu.smartos.health.service.HealthAlertService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "健康预警模块")
@Slf4j
@RestController
@RequestMapping("/api/v1/health/alert")
public class HealthAlertController {
    @Autowired
    private HealthAlertService healthAlertService;

    @ApiOperation("创建健康预警")
    @ApiOperationSupport(order = 10)
    @PostMapping("/create")
    public JsonResult createAlert(@RequestBody HealthAlertParam alertParam) {
        log.info("创建健康预警，老人ID：{}", alertParam.getElderlyUserId());
        Long alertId = healthAlertService.createAlert(alertParam);
        return JsonResult.ok(alertId);
    }

    @ApiOperation("获取家属的所有预警通知")
    @ApiOperationSupport(order = 20)
    @GetMapping("/list/{familyUserId}")
    public JsonResult listAlerts(@PathVariable Long familyUserId) {
        log.info("获取家属用户ID：{}的所有预警通知", familyUserId);
        List<HealthAlertVO> alerts = healthAlertService.listAlertsByFamilyUserId(familyUserId);
        return JsonResult.ok(alerts);
    }

    @ApiOperation("获取家属的未处理预警通知")
    @GetMapping("/unhandled/{familyUserId}")
    public JsonResult listUnhandledAlerts(@PathVariable Long familyUserId) {
        log.info("获取家属用户ID：{}的未处理预警通知", familyUserId);
        List<HealthAlertVO> alerts = healthAlertService.listUnhandledAlertsByFamilyUserId(familyUserId);
        return JsonResult.ok(alerts);
    }

    @ApiOperation("获取预警详情")
    @GetMapping("/detail/{alertId}")
    public JsonResult getAlertDetail(@PathVariable Long alertId) {
        log.info("获取预警ID：{}的详情", alertId);
        HealthAlertVO alert = healthAlertService.getAlertDetail(alertId);
        return JsonResult.ok(alert);
    }

    @ApiOperation("处理预警")
    @PostMapping("/handle/{alertId}")
    public JsonResult handleAlert(@PathVariable Long alertId,
                                  @RequestParam Long handleUserId,
                                  @RequestParam(required = false) String handleComment) {
        log.info("处理预警ID：{}，处理人ID：{}", alertId, handleUserId);
        boolean result = healthAlertService.handleAlert(alertId, handleUserId, handleComment);
        return JsonResult.ok(result);
    }
}